# -*- coding:utf-8 -*-
import datetime
from . import db, Case

OPERATOR_CREATE = 0
OPERATOR_PUSH = 1
OPERATOR_RECEIVE = 2
OPERATOR_UNDO = 3
OPERATOR_RETURN = 4
OPERATOR_MODIFY = 5

def addAction(caseId, userId, type, content = ''):
    t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    db.insert('action', case_id = caseId, user_id = userId, type = type, operate_time = t,content = content)
    Case.updateCaseActionTime(caseId, t)

def caseActions(caseId):
    return list(db.query('SELECT * FROM action WHERE case_id = $caseId ORDER BY operate_time ASC', locals()))

def caseActionsWithUserInfo(caseId):
    return list(db.query('SELECT action.id, user_id, nickname, operate_time, action.type as action_type, content FROM action LEFT JOIN user ON user.id = action.user_id WHERE case_id = $caseId', locals()))

def clear():
    db.query('delete from action')